package com.xjgc.common.utils;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.beanutils.PropertyUtils;

import java.beans.PropertyDescriptor;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Slf4j
public class BetValUtil {

    public static Date parseDate(String dt) throws ParseException {
        Date date=null;
        SimpleDateFormat spt = new  SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        /*if(dt.contains("_begin")){
            date = spt.parse(dt+".000");
        }else if(dt.contains("_end")){
            date = spt.parse(dt+".999");
        }*/
        date = spt.parse(dt);
        return date;
    }
    public static Date fomat() throws ParseException {
        Date date=new Date();
        SimpleDateFormat spt = new  SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return parseDate(spt.format(date));
    }

    public static Map<String, String> betValue( Object searchObj, Map<String, String[]> parameterMap) {
        PropertyDescriptor origDescriptors[] = PropertyUtils.getPropertyDescriptors(searchObj);
        String name,type, beginKey = null,endKey = null;
        String endValue = null,beginValue = null;
        Map<String, String> map = new HashMap<>();
        for (int i = 0; i < origDescriptors.length; i++) {
            name = origDescriptors[i].getName();
            /*type = origDescriptors[i].getPropertyType().toString();*/
            if (parameterMap != null && parameterMap.containsKey(name + "_begin")) {
                beginValue = parameterMap.get(name + "_begin")[0].trim();
                beginKey = name + "_begin";
                map.put(beginKey,beginValue);
            }
            if (parameterMap != null && parameterMap.containsKey(name + "_end")) {
                endValue = parameterMap.get(name + "_end")[0].trim();
                endKey = name + "_end";
                map.put(endKey,endValue);
            }
            if (parameterMap != null && parameterMap.containsKey(name)) {
                endValue = parameterMap.get(name)[0].trim();
                endKey = name;
                map.put(endKey,endValue);
            }
        }

        return map;
    }

    public static QueryWrapper toUnder( Object searchObj, Map<String, String[]> parameterMap) throws ParseException {
        PropertyDescriptor origDescriptors[] = PropertyUtils.getPropertyDescriptors(searchObj);
        String name,type;
        String endValue = null,beginValue = null;
        QueryWrapper<?> queryWrapper = new QueryWrapper<>();
        for (int i = 0; i < origDescriptors.length; i++) {
            name = origDescriptors[i].getName();
            type = origDescriptors[i].getPropertyType().toString();
            if (parameterMap != null && parameterMap.containsKey(name + "_begin")) {

                beginValue = parameterMap.get(name + "_begin")[0].trim();
                Object obj = addQueryByRule(name,type,endValue);
                queryWrapper.ge(camelToUpperline(name),obj);
            }
            if (parameterMap != null && parameterMap.containsKey(name + "_end")) {
                endValue = parameterMap.get(name + "_end")[0].trim();
                Object obj = addQueryByRule(name,type,endValue);
                queryWrapper.ge(camelToUpperline(name),obj);
            }
            if (parameterMap != null && parameterMap.containsKey(name)) {
                endValue = parameterMap.get(name)[0].trim();
                Object obj = addQueryByRule(name,type,endValue);
                queryWrapper.eq(camelToUpperline(name),obj);
            }
        }

        return queryWrapper;
    }
    private static Object addQueryByRule(String name,String type,String value) throws ParseException {
        if(!"".equals(value)) {
            Object temp;
            switch (type) {
                case "class java.lang.Integer":
                    temp =  Integer.parseInt(value);
                    return temp;
                case "class java.math.BigDecimal":
                    temp =  new BigDecimal(value);
                    return temp;
                case "class java.lang.Short":
                    temp =  Short.parseShort(value);
                    return temp;
                case "class java.lang.Long":
                    temp =  Long.parseLong(value);
                    return temp;
                case "class java.lang.Float":
                    temp =   Float.parseFloat(value);
                    return temp;
                case "class java.lang.Double":
                    temp =  Double.parseDouble(value);
                    return temp;
                case "class java.util.Date":
                    temp = parseDate(value);
                    return temp;
                default:
                    temp = value;
                    return temp;
            }
        }
        return null;
    }
    /**
     * 将驼峰命名转化成下划线(转为大写)
     * @param para
     * @return
     */
    public static String camelToUpperline(String para){
        if(para.length()<3){
            return para.toLowerCase();
        }
        StringBuilder sb=new StringBuilder(para);
        int temp=0;//定位
        //从第三个字符开始 避免命名不规范
        for(int i=2;i<para.length();i++){
            if(Character.isUpperCase(para.charAt(i))){
                sb.insert(i+temp, "_");
                temp+=1;
            }
        }
        sb.insert(0,"\"");
        sb.append("\"");
        return sb.toString().toUpperCase();
    }
}
